<template>
    <div class="theme-list">
        <div class="theme-list-card">
            <Row :gutter="10" style="padding: 10px 0;">
                <Col span="5">
                    <Input clearable v-model="searchData.name" placeholder="主题名称" type="text"></Input>
                </Col>
                <Col span="5">
                    <Select @on-change="changeSelect" clearable v-model="searchData.goodsTypeIdOne" placeholder="所属分类">
                        <Option :value="-1">首页</Option>
                        <Option v-for="(key,value) in goodsTypeOne" :value="key.catid">{{key.catname}}</Option>
                    </Select>
                </Col>
                <Col span="5">
                    <Select clearable v-model="searchData.isShow" placeholder="选择隐藏展示">
                        <Option :value="1">展示</Option>
                        <Option :value="2">隐藏</Option>
                    </Select>
                </Col>
                <Col span="9">
                    <div class="paixuCon">
                        <div>
                            <a>排序</a>
                        </div>
                        <div>
                            <i-switch v-model="isswitch" size="large">
                                <span slot="open">开启</span>
                                <span slot="close">关闭</span>
                            </i-switch>
                        </div>
                    </div>
                    <router-link style="float: right;margin-left: 10px" :to="{name:'theme-list-edit-nocache'}">
                        <Button class="subSearch" type="primary"><Icon type="plus-round"></Icon> 新增主题</Button>
                    </router-link>
                    <Button style="float: right" :loading="search_loading" @click="searchGetList" type="primary">
                        <Icon type="ios-pulse-strong"></Icon> 筛 选
                    </Button>
                </Col>
            </Row>
            <DragableTable
                    v-if="isDrage"
                    v-model="data"
                    :columns-list="columns"
                    height="670"
                    @on-end="handleOnend1"
            ></DragableTable>
            <i-table v-else height="670" stripe :columns="columns" :data="data"></i-table>
            <div style="padding: 15px;">
                <Page @on-page-size-change="changeTiao" @on-change="changePage" :current="index" :total="total" show-elevator show-sizer :placement="'top'" :page-size="page_con" :page-size-opts="[100,150,200]"></Page>
            </div>
        </div>
        <Modal v-model="modal2" width="360">
            <p slot="header" style="color:#f60;text-align:center">
                <Icon type="information-circled"></Icon>
                <span>删除确认</span>
            </p>
            <div style="text-align:center">
                <p>是否继续删除？</p>
            </div>
            <div slot="footer">
                <Button type="error" size="large" long :loading="modal_loading" @click="del">删除</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import DragableTable from '../../../tables/components/dragableTable.vue';
    export default {
        name: "theme-list",
        components: {
            DragableTable
        },
        data(){
            return{
                isswitch:false,
                isDrage:false,
                columns:[
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align:"center",
                    },
                    {
                        title: '主题图标',
                        key: 'pic',
                        align:"center",
                        render:(h,params)=>{
                            if(params.row.pic==""){
                                return "暂无"
                            }else{
                                return h("div",{
                                    style:{
                                        cursor: "pointer",
                                    },
                                    on:{
                                        mouseover:(e)=>{
                                            this.$store.commit("setImgData",{
                                                src:e.target.src||e.target.getElementsByTagName("img")[0].src||"",
                                                x:e.x+50+"px",
                                                y:e.y-60+"px",
                                            })
                                        },
                                        mousemove:(e)=>{
                                            this.$store.commit("setImgData",{
                                                src:e.target.src||e.target.getElementsByTagName("img")[0].src||"",
                                                x:e.x+50+"px",
                                                y:e.y-60+"px",
                                            })
                                        },
                                        mouseout:(e)=>{
                                            this.$store.commit("setImgData",{
                                                src:"",
                                                x:"0px",
                                                y:"0px",
                                            })
                                        }
                                    }
                                },[
                                    h("img",
                                        {
                                            attrs:{
                                                src:params.row.pic
                                            },
                                            style:{
                                                width:"42px",
                                                height:"42px",
                                                borderRadius:"4px",
                                                margin:"0px auto",
                                                display:"block",
                                            },
                                        })
                                ])
                            }
                        }
                    },
                    {
                        title: '主题名称',
                        key: 'name',
                        align:"center",
                        render:(h,params)=>{
                            return h("Tooltip",{
                                props:{
                                    placement:"top-start",
                                }
                            },[
                                h("span",{
                                    attrs:{
                                        "class":"notwrap"
                                    },
                                    style:{
                                        cursor:"pointer",
                                        width:"100%",
                                    }
                                },params.row.name),
                                h("div",{
                                    slot:"content",
                                    style:{
                                        "white-space":"normal"
                                    }
                                },params.row.name+"")
                            ])
                        }
                    },
                    {
                        title: '主题分类',
                        key: 'catName',
                        align:"center",
                    },
                    {
                        title: '是否展示',
                        key: 'type',
                        align:"center",
                        render:(h,params)=>{
                            return h("div",
                                [
                                    h("Icon",{
                                        props:{type: params.row.type==1?'eye':'eye-disabled'}
                                    }),
                                    h("span",params.row.type==1?' 展示':' 隐藏')
                                ]
                            )
                        }
                    },
                    {
                        title: '排序',
                        key: 'sort',
                        align:"center",
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        render: (h, params) => {
                            return h('ButtonGroup', [
                                h('Button', {
                                    props: {
                                        type: 'warning',
                                    },
                                    on: {
                                        click: () => {
                                            this.tiao(params.index)
                                        }
                                    }
                                },"修改"),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                    },
                                    on: {
                                        click: () => {
                                            this.remove(params.index)
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    },
                ],
                data:[],
                total:0,
                index:1,
                page_con:100,
                searchData:{
                    name:"",//主题名
                    goodsTypeIdOne:"",
                    isShow:"",//隐藏和显示的赛选
                },
                search_loading:false,
                modal2:false,
                doIndex:0,//操作的下标
                modal_loading:false,
                goodsTypeOne:[],//一级分类
            }
        },
        watch:{
            isswitch(val){
                if(val){
                    if(this.searchData.goodsTypeIdOne==""){
                        this.$Message.warning("请先选择商品分类")
                        this.$nextTick(()=>{this.isswitch = false})
                    }else{
                        this.isDrage=true
                        this.columns.splice(7,1,
                            {
                                title: '拖拽',
                                key: 'drag',
                                width: 90,
                                align: 'center',
                                render: (h) => {
                                    return h(
                                        'Icon',
                                        {
                                            props: {
                                                type: 'arrow-move',
                                                size: 24
                                            }
                                        }
                                    );
                                }
                            })
                    }
                }else{
                    this.columns.splice(7,1)
                    this.isDrage=false
                }
            }
        },
        activated(){
            this.getGoodsPingjiaList()
            this.getTypeOneList()
        },
        methods:{
            getGoodsPingjiaList(){
                this.getJsonData("Zhuti/getAll",{
                    num:this.page_con,
                    page:this.index,
                    name:this.searchData.name,
                    catId:this.searchData.goodsTypeIdOne,
                    type:this.searchData.isShow,
                },true)
                    .then((res) => {
                        this.search_loading=false
                        this.data=res.data
                        this.total=parseInt(res.count)
                    })
                    .catch((res)=>{
                        this.search_loading=false
                    })
            },
            //获取商品一级分类
            getTypeOneList(){
                this.getJsonData("Cat/getOneCat")
                    .then((res) => {
                        this.goodsTypeOne=res.data
                    })
            },
            changePage(n){
                this.index=n
                this.getGoodsPingjiaList()
            },
            changeTiao(num){
                this.page_con=num
                this.index=1
                this.getGoodsPingjiaList()
            },
            searchGetList(){
                this.search_loading=true
                this.getGoodsPingjiaList()
                this.index=1
            },
            remove(n){
                this.doIndex=n
                this.modal2=true
            },
            del(){
                this.modal_loading = true;
                this.getJsonData("Zhuti/deleteZhuti",{id:this.data[this.doIndex].id},true)
                    .then((res)=>{
                        this.getGoodsPingjiaList()
                        this.modal_loading = false;
                        this.modal2 = false;
                        this.$Message.success('删除成功');
                    }).catch((res)=>{
                    this.modal_loading = false;
                    this.modal2 = false;
                })
            },
            tiao(n){
                this.$router.push({name:"theme-list-edit-nocache",query:{id:this.data[n].id,isEdit:true,}})
            },
            //拖拽结果
            handleOnend1 () {
                let arryData = this.data.map((item,key,arr)=>{
                    return `${item.id}@${key}`
                }).join("-")
                this.getJsonData("Zhuti/changePaixu",{
                    str:arryData,
                },true)
                    .then((res) => {
                        this.getGoodsPingjiaList()
                    }).catch(res=>{
                        this.getGoodsPingjiaList()
                })
            },
            changeSelect(value){
                this.getGoodsPingjiaList()
                if(value === "")this.isswitch = false
            }
        }
    }
</script>

<style lang="less">
    @import "theme-list";
</style>